package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.json.JsonUtil;
import com.sirap.basic.json.MistUtil;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XCodeUtil;
import com.sirap.common.domain.Album;

public class PhoenixImageFetchers {
	public static void main(String[] args) {
		Object va;
//		va = getAlbums();
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbumsByPage(8219);
		va = getAlbum("http://news.ifeng.com/a/20181030/60138740_0.shtml#p=1");
//		va = getAlbum("http://news.ifeng.com/photo/zairenjian/detail_2014_04/17/35842225_0.shtml#p=1");
		va = countOfImages("http://news.ifeng.com/a/20180726/59417074_0.shtml#p=1");
		D.pjsp(va);
	}
	
	public static List<Map<String, Object>> getAlbums() {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String urlsports = "http://news.ifeng.com/photo/zairenjian/index.shtml";
				showFetching();
				return urlsports;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/feng.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {
				String regex = "var listObjCmpp = \\'([^\\']+)\\'";
				String data = StrUtil.findFirstMatchedItem(regex, source);
//				C.pl(data);
				Object mist = MistUtil.ofJsonText(data).getCore();
				Map mars = (Map)mist;
				Iterator it = mars.keySet().iterator();
				while(it.hasNext()) {
					Object key = it.next();
					Object value = mars.get(key);
//					D.pla(key, value);
					D.pl(key);
					List months = (List)value;
					for(Object month : months) {
						List mon = (List)month;
						if(mon.isEmpty()) {
							continue;
						}
//						D.list(mon);
						Object kid = MistUtil.ofMapOrList(mon).getCore();
//						D.pl(kid);
						List<Map> toys = (List<Map>)kid;
						for(Map toy : toys) {
							Object key2 = "summary";
							String good = XCodeUtil.urlDecodeUTF8(toy.get(key2) + "");
//							D.pl(good);
							toy.put(key2, good);
						}
						mexItems.addAll((List)kid);
//						D.pjsp(kid);
					}
//					break;
				}
				Collections.shuffle(mexItems);
//				Collections.sort(mexItems, new Comparator<Map>() {
//
//					@Override
//					public int compare(Map m1, Map m2) {
//						String d1 = m1.get("publishedTime") + "";
//						String d2 = m2.get("publishedTime") + "";
//						return d2.compareTo(d1);
//					}
//				});
			}
		};
		
		return frank.process().getItems();
	}
	
	public static Album getAlbum(String albumurl) {
		if(StrUtil.contains(albumurl, "/zairenjian/")) {
			return getAlbumOldVersion(albumurl);
		} else {
			return getAlbumLatestVersion(albumurl);
		}
	}
	
	private static Album getAlbumLatestVersion(String albumurl) {
		Extractor<Album> frank = new Extractor<Album>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return albumurl;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/fake.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {
				String r1 = JsonUtil.createRegexKey("docName");
				String name = StrUtil.findFirstMatchedItem(r1, source);
				if(name == null) {
					r1 = "docname\\s*=\\s*\"(.+?)\"";
					name = StrUtil.findFirstMatchedItem(r1, source);
				}
//				item.put("title", title);
				
				String regex = "scrollImgs\\[\\d{1,3}\\]\\s*=\\s*\\{(.+?)\\};";
				Matcher ma = createMatcher(regex);
				List<String> keys = StrUtil.split("src,width,height,describe");
				List<Object> listObj = Lists.newArrayList();
				List<String> links = Lists.newArrayList();
				while(ma.find()) {
					String image = ma.group(1);
					String temp = image.trim().replace("''+", "");
					Map<String, String> row = Amaps.newLinkHashMap();
					for(String key : keys) {
						String r2 = key + ":\\s*'([^']*)'";
						String value = StrUtil.findFirstMatchedItem(r2, temp);
						row.put(key, value);
					}
					listObj.add(row);
					links.add(row.get("src"));
				}
				
				item = Album.of(name, links).setTag("zai").setListObj(listObj).setUrl(albumurl);
			}
		};
		
		return frank.process().getItem();
	}
	
	//http://news.ifeng.com/photo/zairenjian/detail_2014_04/17/35842225_0.shtml#p=1
	private static Album getAlbumOldVersion(String albumurl) {
		Extractor<Album> frank = new Extractor<Album>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return albumurl;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/fake.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {
				String r1 = JsonUtil.createRegexKey("docName");
				String name = StrUtil.findFirstMatchedItem(r1, source);
				if(name == null) {
					r1 = "docname\\s*=\\s*\"(.+?)\"";
					name = StrUtil.findFirstMatchedItem(r1, source);
				}
				
				String regex = "pic:\\s*\\[(.+?)\\]";
				String kids = StrUtil.findFirstMatchedItem(regex, source);
				String r2 = "\\{(.+?)\\}";
				Matcher ma = createMatcher(r2, kids);
				List<Object> listObj = Lists.newArrayList();
				List<String> links = Lists.newArrayList();
				while(ma.find()) {
					String mix = ma.group(1);
					Map<String, String> row = Amaps.newConcurrentHashMap();
					row.put("src", getValue("url", mix));
					row.put("describe", getValue("desc", mix));
					listObj.add(row);
					links.add(row.get("src"));
				}
				
				item = Album.of(name, links).setTag("zai").setListObj(listObj).setUrl(albumurl);
			}
			
			private String getValue(String key, String mix) {
				String regex = key + "\\s*:\\s*'([^']*)'";
				String value = StrUtil.findFirstMatchedItem(regex, mix);
				
				return value;
			}
		};
		
		return frank.process().getItem();
	}

	public static Integer countOfImages(String albumurl) {
		if(StrUtil.contains(albumurl, "/zairenjian/")) {
			return countOfImagesOldVersion(albumurl);
		} else {
			return countOfImagesLatestVersion(albumurl);
		}
	}
	
	public static Integer countOfImagesOldVersion(String albumurl) {
		Extractor<Integer> frank = new Extractor<Integer>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "url\\s*:\\s*'";
				Matcher ma = createMatcher(regex);
				item = 0;
				while(ma.find()) {
					item++;
				}
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Integer countOfImagesLatestVersion(String albumurl) {
		Extractor<Integer> frank = new Extractor<Integer>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "scrollImgs\\[\\d{1,3}\\]";
				Matcher ma = createMatcher(regex);
				item = 0;
				while(ma.find()) {
					item++;
				}
			}
		};
		
		return frank.process().getItem();
	}
}
